package booyah.validation;

import booyah.application.Position;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

public class PositionValidator {
    public static void validate(Position position, Errors errors) {
        if (!StringUtils.hasLength(position.getName())) {
            errors.rejectValue("name", "required", "required");
        }
        if (!StringUtils.hasLength(position.getDescription())) {
            errors.rejectValue("description", "required", "required");
        }
        if (!StringUtils.hasLength(position.getLocation())) {
            errors.rejectValue("location", "required", "required");
        }
        if (position.getCount() <= 0) {
            errors.rejectValue("count", "required", "required");
        }
        if (position.getCount() < position.getVolunteers().size()) {
            errors.rejectValue("count", "required", "Position < # volunteers");
        }
        if (position.getStartTime() == null) {
            errors.rejectValue("startTime", "required", "required");
        }
        if (position.getEndTime() == null) {
            errors.rejectValue("endTime", "required", "required");
        }
        if ((position.getStartTime() != null) && (position.getEndTime() != null)) {
            if (position.getStartTime().after(position.getEndTime())) {
                errors.rejectValue("startTime", "required", "after End Time");
                errors.rejectValue("endTime", "required", "before Start Time");
            }
        }
    }
}
